//https://leetcode.cn/problems/minimum-operations-to-reduce-x-to-zero/submissions/586496361/
class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int sum = 0;
        int n = nums.size();
        for (auto tmp : nums)sum += tmp;
        sum -= x;
        int left = 0, right = 0, ret = -1, tmp = 0;
        while (right < n)
        {
            sum -= nums[right++];
            while (sum < 0 && left < right) { sum += nums[left++]; }
            if (sum == 0)ret = max(ret, right - left);
        }
        return ret == -1 ? ret : n - ret;
    }
};